<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="NFT-ID">
          <el-input v-model="form.nft_id" placeholder="NFT-ID" />
        </el-form-item>

        <el-form-item label="NFT类型">
          <el-select v-model="form.nft_type" placeholder="NFT类型">
            <el-option
              v-for="item in nftTypeList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发行状态">
          <el-select v-model="form.issue_status" placeholder="发行状态">
            <el-option
              v-for="item in nftStatus"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发行类型">
          <el-select v-model="form.issue_category" placeholder="发行类型">
            <el-option
              v-for="item in nftCategory"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发行平台">
          <el-select v-model="form.issue_platform" placeholder="发行平台">
            <el-option
              v-for="item in nftPlatform"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文件类型">
          <el-select v-model="form.issue_type" placeholder="文件类型">
            <el-option
              v-for="item in nftFilesType"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" />
          <el-table-column prop="nft_id" label="NFT-ID" align="center" />
          <el-table-column prop="nft_name" label="NFT名称" align="center" />
          <el-table-column label="NFT图片" align="center" width="100">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.nft_image"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.nft_image]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column prop="author_uid" label="NFT作者ID" align="center" />
          <el-table-column
            prop="author_name"
            label="NFT作者名称"
            align="center"
          />
          <el-table-column prop="nft_type" label="NFT类型" align="center" />
          <el-table-column prop="nft_status" label="NFT状态" align="center" />
          <el-table-column prop="is_on_chain" label="是否上链" align="center" />
          <el-table-column
            prop="issue_price"
            label="发行钻石价"
            align="center"
          />
          <el-table-column
            prop="issue_status"
            label="发行状态"
            align="center"
          />
          <el-table-column
            prop="issue_category"
            label="发行类别"
            align="center"
          />
          <el-table-column
            prop="issue_platform"
            label="发行平台"
            align="center"
          />
          <el-table-column prop="issue_type" label="文件类型" align="center" />
          <el-table-column
            prop="issue_image"
            label="发行图片"
            align="center"
            width="100"
          >
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.issue_image"
                alt=""
                fit="fill"
                :preview-src-list="[scope.row.issue_image]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            prop="issue_at"
            label="成功发行时间"
            align="center"
          />
          <el-table-column prop="vote_num" label="投票次数" align="center" />
          <el-table-column
            prop="vote_stop_at"
            label="投票截止时间"
            align="center"
          />
          <el-table-column prop="vote_no" label="投票流水号" align="center" />
          <el-table-column
            prop="vote_progress"
            label="投票进度"
            align="center"
          />
          <el-table-column prop="createtime" label="创建时间" align="center"  :formatter="formatterTime"
          width="180"/>
          <el-table-column
            label="操作"
            align="center"
            fixed="right"
            width="140"
          >
            <template #default="scope">
              <el-button
                type="primary"
                plain
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel } from "@/utils/utils";
import { nftListApi,editNFTListApi } from "@/api/moby/nft/nft";
import DialogForm from "./components/dialogForm.vue";
const loading = ref(false);
const tableData = ref([]);
const btnLoading = ref(false);
const dialogVisible = ref(false);
// NFT类型
const nftTypeList = [
  { type: 1, name: "图片" },
  { type: 2, name: "视频" },
  { type: 3, name: "音频" },
  { type: 4, name: "文章" },
  { type: 5, name: "游戏" },
  { type: 6, name: "道具" },
];
// 发行状态
const nftStatus = [
  { type: 0, name: "未发行" },
  { type: 1, name: "已发行" },
  { type: 2, name: "投票中" },
  { type: 3, name: "投票失败" },
];
// 发行类型
const nftCategory = [
  { type: "Item", name: "Item" },
  { type: "Emoji", name: "Emoji" },
  { type: "Avatar", name: "Avatar" },
  { type: "Music", name: "Music" },
  { type: "Video", name: "Video" },
];
// 发行平台
const nftPlatform = [
  { type: "MetaFun", name: "MetaFun" },
  { type: "LinkV", name: "LinkV" },
];
//文件类型
const nftFilesType = [
  { type: "Image", name: "Image" },
  { type: "Picture", name: "Picture" },
  { type: "Video", name: "Video" },
  { type: "Audio", name: "Audio" },
];
const formData = {
  nft_id: "",
  nft_type: "",
  issue_status: "",
  issue_category: "",
  issue_platform: "",
  issue_type: "",
};
const form = reactive({ ...formData });

const dialogFormData = {
  id: "",
  issue_status: "",
};
const dialogForm = reactive({ ...dialogFormData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    nft_id: form.nft_id,
    nft_type: form.nft_type,
    issue_status: form.issue_status,
    issue_category: form.issue_category,
    issue_platform: form.issue_platform,
    issue_type: form.issue_type,
  };
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await nftListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 审核操作
const handleEdit = async (index, row) => {
  dialogVisible.value = true;
  Object.assign(dialogForm, {
    id: row.id,
  });
};
//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    const res = await editNFTListApi(param);
    ElMessage({
      showClose: true,
      message: res.message,
      type: "success",
    });
    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};

// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await nftListApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    nft_id: "NFT ID",
    nft_name: "NFT名称",
    nft_image: "NFT图片",
    author_uid: "NFT作者ID",
    author_name: "NFT作者名称",
    nft_type: "NFT类型",
    nft_status: "NFT状态",
    is_on_chain: "是否上链",
    issue_price: "发行钻石价",
    issue_status: "发行状态",
    issue_category: "发行类别",
    issue_platform: "发行平台",
    issue_type: "文件类型",
    issue_image: "发行图片",
    issue_at: "成功发行时间",
    vote_num: "投票次数",
    vote_stop_at: "投票截止时间",
    vote_no: "投票流水号",
    vote_progress: "投票进度",
    createtime: "创建时间",
  };
  exportToExcel(res.data.results, head, "Moby-NFT发行列表");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
